#include "akWorker.h"

#include <QUdpSocket>
#include <QNetworkInterface>
#include <QHostInfo>

#include "airkiss.h"

akWorker::akWorker()
{
    isrun = true;
}

akWorker::~akWorker()
{
    isrun = false;
    wait();
}

void akWorker::run()
{
    QUdpSocket *ser;
    QHostAddress ha("192.168.1.127");
    QHostInfo hi;
    char buf[512];
    airkiss_context_t ac;

    ser = new QUdpSocket;
    if (!ser->bind(QHostAddress::AnyIPv4, 10000, QUdpSocket::ShareAddress))
    {
        qDebug("bind fail");
        goto _out;
    }

    airkiss_init(&ac, NULL);

    while (isrun)
    {
        int len;

        if (ser->hasPendingDatagrams())
        {
            len = ser->pendingDatagramSize();
            qDebug("len %d", len);
            ser->readDatagram(buf, len);
            airkiss_recv(&ac, NULL, len);
        }

    }

_out:
    delete ser;
}
